<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page import="java.io.IOException"%>
<%@page import="it.cpyx.server.db.TextData"%>
<%@page import="it.cpyx.server.db.utils.TextDataUtils"%>
<%@page import="com.google.appengine.api.users.UserService"%>
<%@page import="com.google.appengine.api.users.UserServiceFactory"%>
<%@page import="com.google.appengine.api.users.User"%>

<%@page import="javax.jdo.PersistenceManager"%>
<%@page import="it.cpyx.server.db.utils.PMF"%>
<%@page import="it.cpyx.server.search.SearchJanitor"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Iterator"%><html>
<%
if(request.getPathInfo().equals("/")){
	try {
		response.sendRedirect("/home");
	} catch (IOException e) {
		e.printStackTrace();
	}
	
}else{
	String urlEx=request.getPathInfo().replaceAll("/","");
	
	TextData t=TextDataUtils.getByUrlExtension(urlEx);
	
	
	
	boolean passChk=false;
	boolean passChked=false;
	if(t!=null){
		if(t.getPass()!=null){
			if(request.getParameter("pass")!=null){
				if(t.getPass().equals(request.getParameter("pass"))){
					passChk=true;
				}else{
					passChked=true;
				}
			}
		}
		if(t.getPass()==null){
			passChk=true;
		}
	}
	
%>
			<head>
				<meta name="google-site-verification" content="-rdtDYfVJU6Pafik3SPvPeMKsvX9PyboVpVPI5TQ44g" />
				<title><%if(t!=null){ %><%=t.getTitle() %><%}else {%>Text Could NOT Found!<%} %> | CpyX.IT Easy Way to Share Your Texts</title>
				<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
			</head>
			<body>
			BETA!!<br>
			<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-5921292-11']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
				<a href="/">home</a>
				<hr>
				<form action="/sh" method="get">
		      <div><input name="q"></input></div>
		      <div><input type="submit" value="Search" /></div>
		    </form>
		    <hr>
		    
		<%
			UserService userService = UserServiceFactory.getUserService();
			User user = userService.getCurrentUser(); 
			if (user != null) {%>
				<div>Hello, <%=user.getNickname()%>! (You can <a href="<%=userService.createLogoutURL(request.getRequestURI())%>">sign out</a>.)</div>
		<%
			} else {
		%>
				<div>Hello! <a href="<%=userService.createLoginURL(request.getRequestURI())%>">Sign in</a>.</div>
		<%
			}
		%>	<hr>
		
		<%
		//Text Found
			if(t!=null ){
				
				boolean chkUsr=false;
				if(t.getOwner()!=null && user!=null){
					if(t.getOwner().equals(user)){
						chkUsr=true;
					}
				}
				if(passChk || chkUsr){
				
					TextDataUtils.updateTextsHitAndLastEntry(t.getId());
				%>
					<div>
						<div >id=<%=t.getId() %></div>
						<div>title=<%=t.getTitle() %></div>
						<div>urlEx=<%=t.getUrlExtension() %></div>
						<div>hit=<%=t.getHit()%></div>
						<div>pass=<%=t.getPass() %></div>
						<div>owner=<%if(t.getOwner()==null){ %>unknown<%}else{ %><%=t.getOwner().getNickname() %><%} %></div>
						<div>date=<%=t.getDate()%></div>
						<div>lastEntry=<%=t.getLastEntry()%></div>
						<div>Content=</div>
						<hr>
						<div><%=t.getContents().getValue()%></div>
						<hr>
						<%
						
						
						Iterator<String> itr = t.getFts().iterator();				
						
						while(itr.hasNext()){
							%><%=itr.next() %>,<%
						}
						
						%>
						<hr>
						
					</div>
			<%}else if(!passChk){
					%><%if(passChked){ %>
					<font color="red">Password invalid!!</font>
					<%} %>
					<form action="/<%=urlEx%>" method="post">
			    	<div>pass:</div>
			    	<div><input name="pass"/></div>
			    	<div><input type="submit" value="CpyX.IT" /></div>
		    </form>
				<%}
		
		//Text Not Found
		}else{
		%>
					<div class="warnT" id="res">Text Could NOT Found!!</div>
			<%}%>
		<%}%>
			</body>
		</html>